package demo1;

import java.util.Stack;

class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(minStack.isEmpty()) {
            minStack.push(val);
        }else {
            if(minStack.peek() >= val) {
                minStack.push(val);
            }
        }
    }

    public void pop() {
        if(stack == null) {
            return;
        }
        int a = stack.pop();
        if(minStack.peek() == a) {
            minStack.pop();
        }
    }

    public int top() {
        if(stack.isEmpty()) {
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack1 = new MinStack();
        minStack1.push(512);
        minStack1.push(-1024);
        minStack1.push(-1024);
        minStack1.push(512);
        minStack1.pop();
        System.out.println(minStack1.getMin());
        minStack1.pop();
        System.out.println(minStack1.getMin());
        minStack1.pop();
        System.out.println(minStack1.getMin());
//        Stack stack1 = new Stack<>();
//        stack1.peek();

    }
}
